/**
 * MIT License
 * 
 * Copyright (c) 2017 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.tunnel.common.util;

public class ByteArrayUtil {

	/**
	 * 从一组数组中获取整体下标范围的数据
	 * 使用这个方法会导致内存短时间消耗过大，GC无法回收全部内存而出现堆空间不足的异常
	 * 仅用于研究
	 */
	@Deprecated
	public static byte[] getSubArray(int startIndex,int endIndex,byte[] ...arys){
		int durStart = 0;
		int durEnd = 0;
		byte[] subAry = new byte[endIndex-startIndex];
		int subAryCopyIndex = 0;
		for(byte[] ary:arys){
			durStart = durEnd;
			durEnd = durStart+ary.length;
			if(durStart<=startIndex && durEnd>startIndex){
				int copyLen = endIndex-startIndex;
				int aryCanCopyLen = durEnd-startIndex;
				if(copyLen>aryCanCopyLen){
					copyLen = aryCanCopyLen;
				}
				System.arraycopy(ary, startIndex-durStart, subAry, subAryCopyIndex, copyLen);
				startIndex=startIndex+copyLen;
				subAryCopyIndex = subAryCopyIndex+copyLen;
				if(startIndex>=endIndex){
					break;
				}
			}
		}
		
		return subAry;
	}
	
	/**
	 * 分割一个字节数组，按照给定的分割符，分割符必须也是字节数组的形式
	 * 只做一次分割，以第一次遇到的分割符为界
	 */
	public static byte[][] splitOnce(byte[] sourceBytes,byte[] splitFlagBytes){
		int i=0;
		for(;i<sourceBytes.length-splitFlagBytes.length+1;i++){
			if(sourceBytes[i] == splitFlagBytes[0]){
				boolean find = true;
				for(int j=i+1,k=1;j<sourceBytes.length && k<splitFlagBytes.length;j++,k++){
					if(sourceBytes[j] != splitFlagBytes[k]){
						find = false;
					}
				}
				if(find){
					break;
				}
			}
		}
		byte[][] result = new byte[2][];
		if(i<sourceBytes.length-splitFlagBytes.length+1){
			//说明找到了
			result[0] = new byte[i];
			if(result[0].length>0){
				System.arraycopy(sourceBytes, 0, result[0], 0, result[0].length);//不能i+1，否则就包含分隔符的第一位了
			}
			result[1] = new byte[sourceBytes.length-i-splitFlagBytes.length];
			if(result[1].length>0){
				System.arraycopy(sourceBytes, i+splitFlagBytes.length, result[1], 0, result[1].length);//不能i+1，否则就包含分隔符的第一位了
			}
		}else{
			result[0] = sourceBytes;
			result[1] = new byte[0];
		}
		return result;
	}
	
	public static void main(String[] args) {
		/*byte[] ary = getSubArray(1, 3, new byte[]{1,2},new byte[]{3,4,5},new byte[]{6,7});
		for(int a:ary){
			System.out.println(a);
		}*/
		
		byte[] sourceBytes = "2143".getBytes();
		byte[] splitFlagBytes = "2".getBytes();
		byte[][] result = splitOnce(sourceBytes, splitFlagBytes);
		if(result != null){
			System.out.println(new String(result[0]));
			System.out.println(new String(result[1]));
		}
	}
}
